package com.loan.system.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.loan.common.excel.ExcelListener;
import com.loan.common.excel.ExcelResult;
import com.loan.common.exception.ServiceException;
import com.loan.common.helper.LoginHelper;
import com.loan.common.utils.spring.SpringUtils;
import com.loan.system.domain.AddressEntity;
import com.loan.system.domain.BankEntity;
import com.loan.system.domain.vo.AddressImportVo;
import com.loan.system.domain.vo.BankImportVo;
import com.loan.system.service.AddressService;
import com.loan.system.service.BankService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author shiguang on 2022/9/8 9:57
 */
@Slf4j
public class BankImportListener extends AnalysisEventListener<BankImportVo> implements ExcelListener<BankImportVo> {

    private final BankService bankService;

    private final String operName;

    private final Boolean isUpdateSupport;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public BankImportListener(Boolean isUpdateSupport) {
        this.bankService = SpringUtils.getBean(BankService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public ExcelResult<BankImportVo> getExcelResult() {
        return new ExcelResult<BankImportVo>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<BankImportVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

    @Override
    public void invoke(BankImportVo bankImportVo, AnalysisContext analysisContext) {
        //根据code查询地址是否存在
        BankEntity bankEntity = this.bankService.selectByBankNo(bankImportVo.getBankNo());
        try {
            if (ObjectUtil.isNull(bankEntity)) {
                bankEntity = BeanUtil.toBean(bankImportVo, BankEntity.class);


                bankEntity.setCreateBy(operName);
                bankService.insertBank(bankEntity);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、bankNO ").append(bankEntity.getBankNo()).append(" 导入成功");
            } else if (isUpdateSupport) {
                bankEntity.setUpdateBy(operName);
                //根据code修改
                bankService.updateBank(bankEntity);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、bankNO ").append(bankEntity.getBankNo()).append(" 更新成功");
            }else{
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、bankNO ").append(bankEntity.getBankNo()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、bankNO " + bankEntity.getBankNo() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
